
$(function(){
	var loginState = $(".userName").text();
	if(loginState == "null" || loginState==null){
		$(".userName").text("未登录(禁止下载)");
	}else{
		$(".userName").text("欢迎你："+loginState);
	}

	$("#submit").click(function(){
		// document.getElementById("file").files
		console.log($("#files").prop("files"));

		var formData = new FormData();
		// var files = $("#files")[0].files;
		var files = $("#files").prop("files");
		for(var i = 0 ; i < files.length ; i ++){
			formData.append('files',files[i]);
		}

		$.ajax({
			type : 'post',
			enctype:"multipart/form-data",
			contentType:false,
			processData: false,
			async: false,
			cache : false,
			data : formData,
			url : '/uploadFiles',
			//请求成功
			success : function(result) {
				console.log(result);
				if(result["code"] == "200"){
					// 文件上传成功
					for(var i = 0 ; i < files.length ; i ++){
						var fileName = files[i].name;
						var uploadData = files[i].lastModified;
						console.log(fileName)
						console.log(GMTToStr())
						$("<div class=\"fileDetail\">\n" +
							"<span class=\"fileName\">"+fileName+"</span>\n" +
							"<a class='download' href=\"/download?filename="+fileName+"\">下载</a>\n" +
							"</div>").appendTo(".uploadFiles");
					}

				}else{
					console.log("文件上传失败");
				}
			},
			//请求失败
			error: function (e) {
				console.log("文件上传失败");
			}
		});
	});

	$("#files").change(function(){
		$(".selectFiles").children().remove();
		console.log($("#files").prop("files"));
        var files = $("#files").prop("files");
        for(var i = 0 ; i < files.length ; i ++){
			console.log(files[i].name);
			$("<span>"+files[i].name+"</span>").appendTo(".selectFiles");
        }

	});
});

function GMTToStr(){
	let date = new Date();
	console.log(date);
	let Str = date.getFullYear() + '-' +
		(date.getMonth() + 1) + '-' +
		date.getDate() + ' ' +
		(date.getHours()) + ':' +
		date.getMinutes() + ':' +
		date.getSeconds();
	return Str;
}
